/**
**********************************************************************
* @file     main.c
* @brief    对文件的简要说明
* @details  对文件的详细说明
* @author   jcai
* @date     2022-05-02 02:27:22
* @copyright Copyright &copy; 2022 wy1107412334@163.com All rights reserved
* @version  v0.0.1
*
* \n Email:  wy1107412334@163.com
* \n Blog:   None
* \n Github: https://github.com/wy1107412334
* \n Gitee:  https://gitee.com/git.git1107412334.com
*
**********************************************************************
* @note
* None.
**********************************************************************
* @attention
* None.
**********************************************************************
@verbatim
@endverbatim
**********************************************************************
* @par 历史记录
* 1. v0.0.1: jcai, 2022-05-02 02:27:22
*   - 首次编写
*
*<center>Copyright &copy; 2022 wy1107412334@163.com All rights reserved</center>
**********************************************************************
*/

/* 头文件导入区 --------------------------------------------------- */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h>


// 窗口属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// 将要使用的表面
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;

// 事件结构体
SDL_Event event;

// 要显示的文字的字体
TTF_Font *font = NULL;
// 字体颜色
SDL_Color textColor = { 0, 0, 0 };

// 要播放的音乐
Mix_Music *music = NULL;
// 将要使用的音效
Mix_Chunk *scratch = NULL;
Mix_Chunk *high = NULL;
Mix_Chunk *med = NULL;
Mix_Chunk *low = NULL;


SDL_Surface *load_image(const char *file)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;

    // 通过 SDL_imag 模块加载图片
    loadedImage = IMG_Load(file);
    if (!loadedImage)   return NULL;

    optimizedImage = SDL_DisplayFormat(loadedImage);
    SDL_FreeSurface(loadedImage);
    if (optimizedImage)
    {
        // 映射关键色
        uint32_t keycolor = SDL_MapRGB(optimizedImage->format, 0, 0xff, 0xff);
        // 将所有颜色为 RGB(0x00, 0xff, 0xff) 的像素设为透明
        SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, keycolor);
    }

    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface *src, SDL_Surface *dst)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(src, NULL, dst, &offset);
}

int init()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return -1;

    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
                              SDL_SWSURFACE);
    if (!screen)
    {
        SDL_Quit();
        return -2;
    }

    if (TTF_Init() == -1)
    {
        SDL_Quit();
        return -3;
    }

    if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
    {
        TTF_Quit();
        SDL_Quit();
        return -4;
    }

    SDL_WM_SetCaption("Monitor Music", NULL);

#if 0
    // 给窗口背景填充白色
    SDL_FillRect(screen, &screen->clip_rect,
                 SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
#endif

    return 0;
}

int load_files()
{
    // 加载背景图片
    background = load_image("background.png");
    if (!background)    return -1;

    font = TTF_OpenFont("lazy.ttf", 30);
    if (!font)
    {
        SDL_FreeSurface(background);
        return -2;
    }

    // 加载音乐
    music = Mix_LoadMUS("beat.wav");
    if (!music)
    {
        SDL_FreeSurface(background);
        TTF_CloseFont(font);
        return -3;
    }

    // 加载音效
    scratch = Mix_LoadWAV("scratch.wav");
    if (!scratch)
    {
        SDL_FreeSurface(background);
        TTF_CloseFont(font);
        Mix_FreeMusic(music);
        return -4;
    }
    high = Mix_LoadWAV("high.wav");
    if (!high)
    {
        SDL_FreeSurface(background);
        TTF_CloseFont(font);
        Mix_FreeChunk(scratch);
        Mix_FreeMusic(music);
        return -5;
    }
    med = Mix_LoadWAV("medium.wav");
    if (!med)
    {
        SDL_FreeSurface(background);
        TTF_CloseFont(font);
        Mix_FreeChunk(scratch);
        Mix_FreeChunk(high);
        Mix_FreeMusic(music);
        return -6;
    }
    low = Mix_LoadWAV("low.wav");
    if (!low)
    {
        SDL_FreeSurface(background);
        TTF_CloseFont(font);
        Mix_FreeChunk(scratch);
        Mix_FreeChunk(high);
        Mix_FreeChunk(med);
        Mix_FreeMusic(music);
        return -7;
    }

    return 0;
}

void clean_up()
{
    SDL_FreeSurface(background);

    Mix_FreeChunk(scratch);
    Mix_FreeChunk(high);
    Mix_FreeChunk(med);
    Mix_FreeChunk(low);
    Mix_FreeMusic(music);
    Mix_CloseAudio();

    TTF_CloseFont(font);
    TTF_Quit();

    SDL_Quit();
}





/**
**********************************************************************
* @brief    简要说明
* @details  详细说明
* @param    none
* @param[in]     a: 该参数用于输入
* @param[in,out] b: 该参数用于输入和输出
* @param[out]    c: 该参数用于输出
* @retval  none
* @retval  0 函数成功
* @retval  1 函数失败
* @return  返回0成功；否则失败
* @note    none
*/
int main(int argc, char *argv[])
{
    int quit = 0;

    if (init() != 0)
    {
        fprintf(stderr, "SDL初始化失败\n");
        return EXIT_FAILURE;
    }
    if (load_files() != 0)
    {
        fprintf(stderr, "SDL加载 background.png 失败\n");
        return EXIT_FAILURE;
    }

    // 加载背景图
    apply_surface(0, 0, background, screen);

    // 渲染文本
    message = TTF_RenderText_Solid(font,
                   "Press 1, 2, 3, or 4 to play a sound effect",
                   textColor);
    if (!message)
    {
        clean_up();
        return EXIT_FAILURE;
    }
    // 显示消息到屏幕
    apply_surface((SCREEN_WIDTH - message->w) / 2,
                  200,
                  message,
                  screen);
    SDL_FreeSurface(message);

    message = TTF_RenderText_Solid(font, "Press 9/0 to start/stop the music",
                                   textColor);
    if (!message)
    {
        clean_up();
        return EXIT_FAILURE;
    }
    apply_surface((SCREEN_WIDTH - message->w) / 2,
                  300,
                  message,
                  screen);
    SDL_FreeSurface(message);

    // 更新窗口
    if (SDL_Flip(screen) == -1)
    {
        fprintf(stderr, "SDL更新窗口失败\n");
        clean_up();
        return EXIT_FAILURE;
    }

    while (!quit)
    {
        // 处理SDL的事件
        while (SDL_PollEvent(&event))
        {
            // 如果有一个按钮被按下
            if( event.type == SDL_KEYDOWN )
            {
                // 如果按钮1被按下
                if( event.key.keysym.sym == SDLK_1 )
                {
                    // 播放划痕(scratch)特效
                    if( Mix_PlayChannel( -1, scratch, 0 ) == -1 )
                    {
                        clean_up();
                        return EXIT_FAILURE;
                    }
                }
                // 如果按钮2被按下
                else if( event.key.keysym.sym == SDLK_2 )
                {
                    // 播放高点击(hit)特效
                    if( Mix_PlayChannel( -1, high, 0 ) == -1 )
                    {
                        clean_up();
                        return EXIT_FAILURE;
                    }
                }
                // 如果按钮3被按下
                else if( event.key.keysym.sym == SDLK_3 )
                {
                    // 播放中等点击(hit)特效
                    if( Mix_PlayChannel( -1, med, 0 ) == -1 )
                    {
                        clean_up();
                        return EXIT_FAILURE;
                    }
                }
                // 如果按钮4被按下
                else if( event.key.keysym.sym == SDLK_4 )
                {
                    // 播放低点击(hit)特效
                    if( Mix_PlayChannel( -1, low, 0 ) == -1 )
                    {
                        clean_up();
                        return EXIT_FAILURE;
                    }
                }
                // 如果按钮9被按下
                else if( event.key.keysym.sym == SDLK_9 )
                {
                    if( Mix_PlayingMusic() == 0 )
                    {
                        // 如果音乐没有播放，则播放音乐
                        if( Mix_PlayMusic( music, -1 ) == -1 )
                        {
                            clean_up();
                            return EXIT_FAILURE;
                        }
                    }
                    else
                    {
                        if( Mix_PausedMusic() == 1 )
                            // 如果音乐没有播放，则继续播放音乐
                            Mix_ResumeMusic();
                        else
                            // 暂停音乐
                            Mix_PauseMusic();
                    }
                }
                // 按钮0被按下
                else if( event.key.keysym.sym == SDLK_0 )
                    // 停止播放音乐
                    Mix_HaltMusic();
            }
            // 如果用户点击了窗口右上角的关闭按钮
            if (event.type == SDL_QUIT)
            {
                quit = 1;
                printf("处理退出事件\n");
            }
        }
    }

    clean_up();

    return 0;
}




/***  Copyright (C) 2022 wy1107412334@163.com All rights reserved ***/
/****************************** END OF FILE *************************/

